import Character from "./Character";
const {ccclass, property} = cc._decorator;
@ccclass
export default class Corps extends Character{
    /**
     *  敌军团数据 
     * action 2巡逻  3追击
    */
    private _corps:Db.Corps;
    get corps(){
        return this._corps;
    }
    set corps(value){
        this._corps = value;
        this.action = 2;
        this.dir = 0;        
        this.init();
    }

    /** 动画组件 */
    private animCom:cc.Animation;
    /** 出生地 */
    private birthplace:cc.Vec2;


    /** 当前Npc所拥有的动作名称 */
    private animNameRow:string[]=[];


    onLoad(){
        this.node.active = false;
        this.animCom = this.getComponent(cc.Animation);        
    }

    start(){
        this.birthplace = this.node.getPosition();
        this.schedule(this.checkPatrol,1);
    }

    update(){    
                
    }


    /** 初始化Npc */
    init(){      
        this.node.active = true; 
        //加载npc的图片资源
        cc.loader.loadRes("Npc/"+this.corps.texture,cc.SpriteAtlas,(err,atlas:cc.SpriteAtlas)=>{
            //101-108为八方向图        
            for(var i=1;i<=8;i++){
                //添加八方向待机动画
                var sp = atlas.getSpriteFrame("10"+i);
                if(sp){
                    var clip = cc.AnimationClip.createWithSpriteFrames([sp],1);
                    clip.name = "1_"+i;
                    clip.wrapMode = cc.WrapMode.Normal;
                    this.animCom.addClip(clip)    
                    this.animNameRow.push(clip.name);
                }
                //添加八方向行走图 如果当前方向存在图片，则每个方向有4张图片
                var sp1 = atlas.getSpriteFrame("2"+i+"1");
                if(sp1){
                    var sp2 = atlas.getSpriteFrame("2"+i+"2");
                    var sp3 = atlas.getSpriteFrame("2"+i+"3");
                    var sp4 = atlas.getSpriteFrame("2"+i+"4");
                    var sps = [sp1,sp2,sp3,sp4];
                    var clip = cc.AnimationClip.createWithSpriteFrames( sps as [cc.SpriteFrame],4);
                    clip.name = "2_"+i;
                    clip.wrapMode = cc.WrapMode.Loop;
                    this.animCom.addClip(clip);
                    this.animNameRow.push(clip.name);
                }
                //添加默认动作1 如果当前方向存在图片，则每个方向有2张图片
                var asp1 = atlas.getSpriteFrame("3"+i+"1");
                if(asp1){
                    var asp2 = atlas.getSpriteFrame("3"+i+"2");
                    var sps = [asp1,asp2];
                    var clip = cc.AnimationClip.createWithSpriteFrames( sps as [cc.SpriteFrame],2);
                    clip.name = "3_"+i;
                    clip.wrapMode = cc.WrapMode.Loop;
                    this.animCom.addClip(clip);
                    this.animNameRow.push(clip.name);
                }              
            }

            //补几个动画
            this.animNameRow.push("2_6","2_8","3_6","3_8")
      
        })       
        this.zIndex();
        //敌军团实例完成后 开始巡逻
        this.Patrol();
    }

    /**
     * 播放动画
     */
    playAnim(){
        var action = this.action;
        var dir = this.dir;
        var sX=1;
        if(action!=1){         
            switch(dir){
                case 8:
                    dir = 5;
                    sX = -1;
                break;
                case 6:
                    dir = 7;
                    sX = -1;                
                break;
                case 1: 
                    dir = 5;
                    if(! (this.lastDir%2) ){
                        sX=-1
                    }
                break;
                case 2:
                    dir = 7;
                    if(! (this.lastDir%2) ){
                        sX=-1
                    }                    
                break;
                case 3:
                    if(this.lastDir==5||this.lastDir==1){
                        dir = 5
                    }else{
                        dir = 7
                    }
                break;
                case 4:
                    sX = -1;
                    if(this.lastDir==1||this.lastDir==8){
                        dir = 5
                    }else{
                        dir = 7
                    }
                break;
            }
        }

        
        var name = action+"_"+dir;

        if(this.animNameRow.indexOf(name)>-1){
            this.node.scaleX = sX;
            this.animCom.play(name);
        }

    }

    /** 巡逻 */
    Patrol(){
        //选择一个方向进行移动
        var tempD=this.dir;   
       
        while(tempD==this.dir){            
            tempD = RandomInt(1,5);               
        }    
        
   
        this.dir = tempD;
        //设置动作为巡逻移动
        this.action = 2;

        
        
        
        var v2 = cc.v2(QM.getVec2ForDir(this.dir)).normalize();


        var a1 = cc.moveBy(1/60,v2.mul(this.corps.speed));        
   
        var dir = this.dir;
        var sX = 1;
        switch(dir){
            case 8:
                dir = 5;
                sX = -1;
            break;
            case 6:
                dir = 7;
                sX = -1;                
            break;
            case 1: 
                dir = 5;
                if(! (this.lastDir%2) ){
                    sX=-1
                }
            break;
            case 2:
                dir = 7;
                if(! (this.lastDir%2) ){
                    sX=-1
                }                    
            break;
            case 3:
                if(this.lastDir==5||this.lastDir==1){
                    dir = 5
                }else{
                    dir = 7
                }
            break;
            case 4:
                sX = -1;
                if(this.lastDir==1||this.lastDir==8){
                    dir = 5
                }else{
                    dir = 7
                }
            break;
        }
        this.node.scaleX = sX;
        this.animCom.play(this.action+"_"+dir);
        this.node.runAction( cc.repeatForever(a1) );
    }


    checkPatrol(){
        //在巡逻时，判断是否出了边界 | 巡逻范围

        if(this.action==2){
            var p1 = this.node.position
            if(this.birthplace.sub(p1).mag()>this.corps.radius){
                this.node.stopAllActions();
                this.Patrol();
            }
        }
    }

    onCollisionEnter(other:cc.Collider,self){
        cc.log(other);
        if(other.node.group=="wall"){
            this.Patrol();
            return;
        }
    }
   

}
